﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Latin_Vocabulary_Indicator
{
    public partial class profile_saver : Form
    {
        resultbox _caller;
        bool keepDefault = true;
        string dater;
        public profile_saver(resultbox caller)
        {
            InitializeComponent();
            _caller = caller;
            DateTime temp2 = _caller._caller.endTime;
            string folderPath = "Record\\" +temp2.Month.ToString().PadLeft(2, '0') +
                temp2.Day.ToString().PadLeft(2, '0') + temp2.Year.ToString().PadLeft(4, '0');
            short defaultName = 1;
            if (Directory.Exists(folderPath))
            {
                for(short x = 1; true; x++)
                {
                    if (File.Exists(folderPath + "\\" + x + ".txt")) defaultName = (short)(x + 1);
                    else break;
                }
            }
            name_.Text = folderPath + "\\" + defaultName + ".txt";
        }

        private void save_Click(object sender, EventArgs e)
        {
            DateTime temp1 = _caller._caller.startTime;
            DateTime temp2 = _caller._caller.endTime;
            if (keepDefault)
            {
                if (!Directory.Exists("Record")) Directory.CreateDirectory("Record");
                string temp = "Record\\" +temp2.Month.ToString().PadLeft(2, '0') +
                    temp2.Day.ToString().PadLeft(2, '0')+ temp2.Year.ToString().PadLeft(4, '0');
                if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
            }
            string output = _caller._caller.title.Text +"\n";
            output += "\n|Time|\n\n";
            output += " Start time:      " + temp1.Month.ToString().PadLeft(2, '0') + "/"+ temp1.Day.ToString().PadLeft(2, '0') + "/" + temp1.Year+"  "+temp1.Hour.ToString().PadLeft(2,'0')+":"+temp1.Minute.ToString().PadLeft(2, '0') + "\n";
            output += " Complete time:   " + temp2.Month.ToString().PadLeft(2, '0') + "/" + temp2.Day.ToString().PadLeft(2, '0') + "/" + temp2.Year + "  " + temp2.Hour.ToString().PadLeft(2, '0') + ":" + temp2.Minute.ToString().PadLeft(2, '0') + "\n";
            output += "\n|State|\n\n";
            byte longestLength = 0;
            foreach (ListViewItem sub in _caller.resultList.Items) if (sub.Text.Length+1 > longestLength) longestLength =(byte)( sub.Text.Length+1);
            foreach (ListViewItem sub in _caller.resultList.Items)  output +=" "+ (sub.Text + ":").PadRight(longestLength) + "   " + sub.SubItems[1].Text + (sub.Text== "Condition" || sub.Text == "Rotation"?"\n\n": "\n"); 
            output += "\n|Detail|\n\n";
            string outputString ="";
            short number = 1;
            if (_caller._caller._testType == mainproj.testType.review)
            {
                outputString += "No.|Word|pos|English|Reviewed\n||||\n";
                foreach (ListViewItem sub in _caller._caller._examined)
                    outputString += number++ + "|" + sub.SubItems[1].Text + "|" + sub.SubItems[2].Text
                        + "|" + sub.SubItems[3].Text + "|" + sub.SubItems[5].Text + "\n";
            }
            else if (_caller._caller._testType == mainproj.testType.test)
            {
                outputString += "No.|English|Latin|Your answer|State\n||||\n";
                foreach (ListViewItem sub in _caller._caller.detail) 
                    outputString += number++  + "|" +  sub.SubItems[1].Text + "|" + sub.SubItems[2].Text + "|" + sub.SubItems[3].Text + "|" + sub.Tag + "\n";
            }
            byte[] longLength = new byte[5];
            for (byte x = 0; x <= 4; x++) longLength[x] = 0;
            for (short x = 0; x <= outputString.Split('\n').Length - 1; x++)
                for (byte y = 0; y <= outputString.Split('\n')[x].Split('|').Length-1; y++)
                    if (outputString.Split('\n')[x].Split('|')[y].Length > longLength[y])
                        longLength[y] =(byte)( outputString.Split('\n')[x].Split('|')[y].Length);
            for (short x = 0; x <= outputString.Split('\n').Length - 1; x++)
            {
                output += " ";
                for (byte y = 0; y <= outputString.Split('\n')[x].Split('|').Length-1; y++)
                   output += outputString.Split('\n')[x].Split('|')[y].PadRight(longLength[y]) + (y!= outputString.Split('\n')[x].Split('|').Length-1?"   ":null);
                if (x != outputString.Split('\n').Length - 1) output += "\n";
            }
            Func<string, byte, string> turnLine = (centerer, length) =>
              {
                  byte leftCount = (byte)Math.Min(15,(length - centerer.Length) / 2);
                  string putout = "";
                  for (byte x = 0; x <= leftCount - 1; x++) putout += "-";
                  putout += centerer;
                  byte rightCount = (byte)(length - centerer.Length - leftCount);
                  for (byte x = 0; x <= rightCount - 1; x++) putout += "-";
                  return putout;
              };
            byte longest = 0;
            foreach (string sub in output.Split('\n')) if (sub.Length > longest) longest =(byte) sub.Length;
            output = output.Replace("|Time|", turnLine("Time",++longest));
            output = output.Replace("|State|", turnLine("State", longest));
            output = output.Replace("|Detail|", turnLine("Detail", longest));
            fileOperator.textOutput(name_.Text, output.Replace("\n", "\r\n"));
            _caller.save.Enabled = false;
            Close();
        }

        private void browze_Click(object sender, EventArgs e)
        {
            saveFile.ShowDialog();
            if (saveFile.FileName != "")
            {
                keepDefault = false;
                name_.Text = saveFile.FileName;
                saveFile.FileName = "";
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void profile_saver_FormClosing(object sender, FormClosingEventArgs e)
        {
            _caller.Enabled = true;
            _caller.Focus();
        }
    }
}
